package com.zdy.im.user.service.impl;


import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.common.enums.ResultEnum;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.util.PageUtils;
import com.sixeco.framework.util.excel.ExcelUtils;
import com.zdy.im.user.service.ImUserGroupInfoService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


import com.zdy.im.user.entity.ImUserGroupInfo;
import com.zdy.im.user.dao.ImUserGroupInfoDao;
import com.zdy.im.user.dto.request.ImUserGroupInfoRequestDto;
import com.zdy.im.user.dto.response.ImUserGroupInfoResponseDto;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;


/**
 * @ClassName: ImUserGroupInfoDao.java
 * @Description: 用户好友分组表的数据交互处理类
 * @author xxx
 * @date   2021-05-08 17:45:59
 * @version V1.0.0
 * <p>
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 * </p>
 */
@Service
public class ImUserGroupInfoServiceImpl  implements ImUserGroupInfoService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ImUserGroupInfoDao  imUserGroupInfoDao;



    /**
     * 保存用户好友分组表同时更新数据库和缓存的实现方法
     * @param imUserGroupInfoRequestDto ImUserGroupInfoResponseDto
     * @return
     */
    public ImUserGroupInfoResponseDto saveImUserGroupInfo(ImUserGroupInfoRequestDto imUserGroupInfoRequestDto){
        if(imUserGroupInfoRequestDto== null){
            return null;
        }
        ImUserGroupInfo imUserGroupInfo = ImUserGroupInfo.build().clone(imUserGroupInfoRequestDto);
        boolean result = imUserGroupInfoDao.save(imUserGroupInfo);
        if(!result){
            return null;
        }
        ImUserGroupInfoResponseDto imUserGroupInfoResponseDto = ImUserGroupInfoResponseDto.build().clone(imUserGroupInfo);
        return imUserGroupInfoResponseDto;


    }

    public boolean saveBatchImUserGroupInfo(Collection<ImUserGroupInfoRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() !=  batchSize){
            return false;
        }
        List<ImUserGroupInfo> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImUserGroupInfo.build().clone(e)));
        boolean result = imUserGroupInfoDao.saveBatch(list,batchSize);
        return result;
    }


    public boolean saveOrUpdateBatchImUserGroupInfo(Collection<ImUserGroupInfoRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImUserGroupInfo> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImUserGroupInfo.build().clone(e)));
        boolean result = imUserGroupInfoDao.saveOrUpdateBatch(list,batchSize);
        return result;
    }


    public boolean updateBatchById(Collection<ImUserGroupInfoRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImUserGroupInfo> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImUserGroupInfo.build().clone(e)));
        boolean result =  imUserGroupInfoDao.updateBatchById(list,batchSize);
        return result;
    }

    /**
     * 更新用户好友分组表同时更新数据库和缓存的实现方法
     * @param imUserGroupInfoRequestDto  ImUserGroupInfoResponseDto
     * @return
     */
    public boolean updateImUserGroupInfo(ImUserGroupInfoRequestDto imUserGroupInfoRequestDto){

          if(null ==  imUserGroupInfoRequestDto){
              return false;
          }

        ImUserGroupInfo imUserGroupInfo = ImUserGroupInfo.build().clone(imUserGroupInfoRequestDto);

        return imUserGroupInfoDao.updateById(imUserGroupInfo);


    }







    /**
     * 通过主键ID删除对象信息实现缓存和数据库,同时删除的方法
     * @param  idList
     * @return
     */
    public int delImUserGroupInfoByIds(List<Long> idList){
        boolean result = false;
        if(null == idList){
            return ResultEnum.FAIL.id();
        }
        if( idList.size() == 1) {
            result = imUserGroupInfoDao.removeById(idList.get(0));
        }else {
            result =  imUserGroupInfoDao.removeByIds(idList);
        }
        if(result){
            return ResultEnum.SUCCESS.id();
        }
        return ResultEnum.FAIL.id();

    }


    /**
     * 通过主键ID更新对象用户好友分组表实现缓存和数据库更新的方法
     * @param  imUserGroupInfoId
     * @return
     */
    public ImUserGroupInfoResponseDto getImUserGroupInfoById(long imUserGroupInfoId){
        if(imUserGroupInfoId < 0 ){
            return null;
        }
        ImUserGroupInfo imUserGroupInfo =  imUserGroupInfoDao.getById(imUserGroupInfoId);
        if(imUserGroupInfo == null){
            return null;
        }
        ImUserGroupInfoResponseDto imUserGroupInfoResponseDto = ImUserGroupInfoResponseDto.build().clone(imUserGroupInfo);

        return imUserGroupInfoResponseDto ;

    }



    /**
    * 查询条件
    */
    public QueryWrapper<ImUserGroupInfo> checkQueryCondition(BasePage basePage) {
        QueryWrapper<ImUserGroupInfo> queryWrapper = new QueryWrapper<>();
        ImUserGroupInfoRequestDto imUserGroupInfoRequestDto = (ImUserGroupInfoRequestDto) basePage.getParamObject();

                                                                                                                                                            return queryWrapper;
    }


    /**
     * 通过分页获取ImUserGroupInfo信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author xxx
     * @date 2021-05-08 17:45:59
     */
    public List<ImUserGroupInfoResponseDto> getImUserGroupInfoByPage(
            ResponseResultList<ImUserGroupInfoResponseDto> responseResultList, BasePage basePage){


        List<ImUserGroupInfoResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        IPage<ImUserGroupInfo> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        QueryWrapper<ImUserGroupInfo> queryWrapper = checkQueryCondition(basePage);

        IPage<ImUserGroupInfo> page = imUserGroupInfoDao.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImUserGroupInfo>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        list.forEach(imUserGroupInfo -> {
                ImUserGroupInfoResponseDto imUserGroupInfoResponseDto = ImUserGroupInfoResponseDto.build().clone(imUserGroupInfo);

            resDtoList.add(imUserGroupInfoResponseDto);
        });
        responseResultList.setTotal((int) page.getTotal());
        return resDtoList;


    }

    /**
     * 通过分页获取ImUserGroupInfo信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author xxx
     * @date 2021-05-08 17:45:59
     */
    public ResponseResultList<ImUserGroupInfoResponseDto> getImUserGroupInfoByNextPage(BasePage basePage){
        ResponseResultList<ImUserGroupInfoResponseDto> responseResultList = ResponseResultList.build();
        List<ImUserGroupInfoResponseDto>  list = this.getImUserGroupInfoByPage(responseResultList,basePage);
        if(null == list ){
            list = new ArrayList<>();
        }
        boolean isNext =  basePage.isNextPage(list);
        responseResultList.toIsNextPage(isNext).toList(list);
        return responseResultList;

    }

    public PageUtils queryPage(Map<String, Object> params) {
        IPage iPage =  new Query<ImUserGroupInfo>().getPage(params);
        QueryWrapper<ImUserGroupInfo> queryWrapper = new QueryWrapper<>();
        IPage<ImUserGroupInfo> page = imUserGroupInfoDao.page(iPage,queryWrapper);
        return new PageUtils(page);
    }


    /**
     * 保存用户好友分组表同时更新数据库和缓存的实现方法
     * @return
     */
    public ImUserGroupInfo  setImUserGroupInfo(){
        ImUserGroupInfo imUserGroupInfo = new ImUserGroupInfo();
        /**
 			//imUserGroupInfo.setCreateDate (Date createDate);
 			//imUserGroupInfo.setModifyDate (Date modifyDate);
 			//imUserGroupInfo.setStatus (int status);
 			//imUserGroupInfo.setUserId (long userId);
 			//imUserGroupInfo.setGroupName (String groupName);
 			//imUserGroupInfo.setRemarks (String remarks);
 			//imUserGroupInfo.setSort (int sort);
		**/

        return imUserGroupInfo;
    }

    public boolean saveData(InputStream initialStream) {
        return ExcelUtils.readExcel(initialStream,imUserGroupInfoDao, ImUserGroupInfo.class,0);
    }

    public ImUserGroupInfoResponseDto selectDefGroupOne(long userId){
        ImUserGroupInfo imUserGroupInfo =  imUserGroupInfoDao.selectDefGroupOne(userId);
        if(null == imUserGroupInfo){
            return null;
        }
        return ImUserGroupInfoResponseDto.build().clone(imUserGroupInfo);
    }

    public ImUserGroupInfoResponseDto selectGroupName(long userId, String groupName){
        ImUserGroupInfo imUserGroupInfo = imUserGroupInfoDao.selectGroupOneByGroupName(userId,groupName);
        if(imUserGroupInfo == null){
            return null;
        }
        return ImUserGroupInfoResponseDto.build().clone(imUserGroupInfo);
    }

    /**
     * 查询某用户下，所有组信息
     * @param userId
     * @return
     */
    public List<ImUserGroupInfoResponseDto> selectListByUserId(long userId){
        List<ImUserGroupInfo> list = imUserGroupInfoDao.selectListByUserId( userId);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ImUserGroupInfoResponseDto> lists = new ArrayList<>();
        list.forEach(e->{
            lists.add(ImUserGroupInfoResponseDto.build().clone(e));
        });
        return lists;
    }

    /**
     * 查询某用户，组的最大序号
     * @param userId
     * @return
     */
    public ImUserGroupInfoResponseDto getOneByUserId(long userId){
        ImUserGroupInfo imUserFriend = imUserGroupInfoDao.getOneByUserId( userId);
        if(imUserFriend == null){
            return null;
        }
        return ImUserGroupInfoResponseDto.build().clone(imUserFriend);
    }
}
